const SkipList = require("../template")

function testSkipListComplexScenarios() {
  console.log(
    "1. 复杂数据结构测试:",
    (() => {
      const sl = new SkipList()
      
      // 插入复杂数据
      const complexData = [
        { id: 1, value: 100 },
        { id: 2, value: 200 },
        { id: 3, value: 300 }
      ]
      
      complexData.forEach(item => sl.insert(item.id))
      
      // 验证复杂数据
      const allFound = complexData.every(item => sl.search(item.id) === true)
      
      return allFound &&
             sl.getSize() === complexData.length
    })()
  )

  console.log(
    "2. 跳表动态负载测试:",
    (() => {
      const sl = new SkipList()
      
      // 动态负载测试
      const loadPatterns = [
        { operation: 'insert', values: [10, 20, 30, 40, 50] },
        { operation: 'delete', values: [20, 40] },
        { operation: 'insert', values: [25, 35, 45] },
        { operation: 'delete', values: [10, 50] },
        { operation: 'insert', values: [15, 55] }
      ]
      
      loadPatterns.forEach(pattern => {
        if (pattern.operation === 'insert') {
          pattern.values.forEach(val => sl.insert(val))
        } else if (pattern.operation === 'delete') {
          pattern.values.forEach(val => sl.delete(val))
        }
      })
      
      // 验证最终状态
      const finalValues = sl.getAllValues()
      const size = sl.getSize()
      
      return size === 6 &&
             finalValues.includes(15) &&
             finalValues.includes(25) &&
             finalValues.includes(30) &&
             finalValues.includes(35) &&
             finalValues.includes(45) &&
             finalValues.includes(55)
    })()
  )

  console.log(
    "3. 跳表边界条件测试:",
    (() => {
      const sl = new SkipList()
      
      // 边界条件测试
      const boundaryTests = [
        () => sl.insert(Number.MAX_SAFE_INTEGER),
        () => sl.insert(Number.MIN_SAFE_INTEGER),
        () => sl.insert(0),
        () => sl.insert(-1),
        () => sl.insert(1)
      ]
      
      boundaryTests.forEach(test => test())
      
      // 验证边界值
      const maxFound = sl.search(Number.MAX_SAFE_INTEGER)
      const minFound = sl.search(Number.MIN_SAFE_INTEGER)
      const zeroFound = sl.search(0)
      const negFound = sl.search(-1)
      const posFound = sl.search(1)
      
      return maxFound === true &&
             minFound === true &&
             zeroFound === true &&
             negFound === true &&
             posFound === true &&
             sl.getSize() === 5
    })()
  )

  console.log(
    "4. 跳表错误恢复测试:",
    (() => {
      const sl = new SkipList()
      
      // 错误恢复测试
      try {
        // 插入数据
        sl.insert(10)
        sl.insert(20)
        sl.insert(30)
        
        // 删除不存在的值
        sl.delete(999)
        
        // 搜索不存在的值
        const found = sl.search(999)
        
        // 验证错误恢复
        return sl.getSize() === 3 &&
               found === false &&
               sl.search(10) === true &&
               sl.search(20) === true &&
               sl.search(30) === true
      } catch (error) {
        return false
      }
    })()
  )

  console.log(
    "5. 跳表内存管理测试:",
    (() => {
      const sl = new SkipList()
      
      // 内存管理测试
      const dataSize = 1000
      
      // 插入大量数据
      for (let i = 0; i < dataSize; i++) {
        sl.insert(i)
      }
      
      const sizeBefore = sl.getSize()
      const levelBefore = sl.getLevel()
      
      // 删除所有数据
      for (let i = 0; i < dataSize; i++) {
        sl.delete(i)
      }
      
      const sizeAfter = sl.getSize()
      const levelAfter = sl.getLevel()
      
      // 验证内存管理
      return sizeBefore === dataSize &&
             sizeAfter === 0 &&
             levelBefore >= 0 &&
             levelAfter >= 0
    })()
  )

  console.log(
    "6. 跳表并发操作测试:",
    (() => {
      const sl = new SkipList()
      
      // 并发操作测试
      const operations = [
        () => sl.insert(10),
        () => sl.insert(20),
        () => sl.search(10),
        () => sl.insert(30),
        () => sl.delete(20),
        () => sl.search(30),
        () => sl.insert(40),
        () => sl.delete(10)
      ]
      
      operations.forEach(op => op())
      
      // 验证并发操作结果
      const finalValues = sl.getAllValues()
      const size = sl.getSize()
      
      return size === 2 &&
             finalValues.includes(30) &&
             finalValues.includes(40) &&
             !finalValues.includes(10) &&
             !finalValues.includes(20)
    })()
  )

  console.log(
    "7. 跳表复杂查询测试:",
    (() => {
      const sl = new SkipList()
      
      // 插入数据
      const values = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
      values.forEach(val => sl.insert(val))
      
      // 复杂查询测试
      const rangeQuery = sl.getRangeValues(25, 75)
      const rankQuery = sl.getRank(50)
      const minValue = sl.getMin()
      const maxValue = sl.getMax()
      
      // 验证查询结果
      return rangeQuery.length === 6 && // 30, 40, 50, 60, 70
             rankQuery === 5 && // 50的排名
             minValue === 10 &&
             maxValue === 100
    })()
  )

  console.log(
    "8. 跳表状态一致性测试:",
    (() => {
      const sl = new SkipList()
      
      // 状态一致性测试
      const operations = [
        () => sl.insert(10),
        () => sl.insert(20),
        () => sl.insert(30),
        () => sl.delete(20),
        () => sl.insert(25),
        () => sl.delete(10),
        () => sl.insert(15)
      ]
      
      operations.forEach(op => op())
      
      // 验证状态一致性
      const allValues = sl.getAllValues()
      const size = sl.getSize()
      const min = sl.getMin()
      const max = sl.getMax()
      const isEmpty = sl.isEmpty()
      
      return size === 3 &&
             min === 15 &&
             max === 30 &&
             isEmpty === false &&
             allValues.includes(15) &&
             allValues.includes(25) &&
             allValues.includes(30)
    })()
  )

  console.log(
    "9. this上下文复杂场景测试:",
    (() => {
      const complexObj = {
        multiplier: 3,
        processComplex: function(values) {
          const sl = new SkipList()
          values.forEach(val => sl.insert(val))
          return sl.getSize() * this.multiplier
        }
      }
      
      const values = [10, 20, 30, 40, 50]
      return complexObj.processComplex(values) === 15 // 5*3=15
    })()
  )

  console.log(
    "10. 复杂对象this绑定复杂场景测试:",
    (() => {
      const complexConfigObj = {
        config: { threshold: 4 },
        processWithConfig: function(values) {
          const sl = new SkipList()
          values.forEach(val => sl.insert(val))
          return sl.getSize() >= this.config.threshold
        }
      }
      
      const values = [10, 20, 30, 40, 50]
      return complexConfigObj.processWithConfig(values) === true
    })()
  )

  console.log(
    "11. 跳表复杂操作序列测试:",
    (() => {
      const sl = new SkipList()
      
      // 复杂操作序列
      const operationSequence = [
        { op: 'insert', val: 10 },
        { op: 'insert', val: 20 },
        { op: 'insert', val: 30 },
        { op: 'search', val: 20 },
        { op: 'delete', val: 20 },
        { op: 'insert', val: 25 },
        { op: 'search', val: 25 },
        { op: 'delete', val: 10 },
        { op: 'insert', val: 15 },
        { op: 'search', val: 15 }
      ]
      
      let searchResults = []
      
      operationSequence.forEach(seq => {
        if (seq.op === 'insert') {
          sl.insert(seq.val)
        } else if (seq.op === 'delete') {
          sl.delete(seq.val)
        } else if (seq.op === 'search') {
          searchResults.push(sl.search(seq.val))
        }
      })
      
      // 验证操作序列结果
      const finalValues = sl.getAllValues()
      const size = sl.getSize()
      
      return size === 3 &&
             finalValues.includes(15) &&
             finalValues.includes(25) &&
             finalValues.includes(30) &&
             searchResults.every(result => result === true)
    })()
  )

  console.log(
    "12. 跳表复杂场景综合测试:",
    (() => {
      const sl = new SkipList()
      
      // 综合复杂场景
      const scenarios = [
        // 场景1: 大量插入
        () => {
          for (let i = 0; i < 100; i++) {
            sl.insert(i)
          }
        },
        // 场景2: 部分删除
        () => {
          for (let i = 0; i < 50; i += 2) {
            sl.delete(i)
          }
        },
        // 场景3: 重新插入
        () => {
          for (let i = 0; i < 50; i += 2) {
            sl.insert(i)
          }
        },
        // 场景4: 范围查询
        () => {
          const range = sl.getRangeValues(25, 75)
          return range.length === 51
        },
        // 场景5: 排名查询
        () => {
          const rank = sl.getRank(50)
          return rank === 51
        }
      ]
      
      scenarios.forEach(scenario => {
        const result = scenario()
        if (result !== undefined && result !== true) {
          return false
        }
      })
      
      // 验证最终状态
      const finalSize = sl.getSize()
      const finalMin = sl.getMin()
      const finalMax = sl.getMax()
      
      return finalSize === 100 &&
             finalMin === 0 &&
             finalMax === 99
    })()
  )
}

testSkipListComplexScenarios()
